package com.atguigu.binarysearchnorecur;

import com.atguigu.sort.ArrCreator;
import com.atguigu.sort.MergeSort;

import java.util.Arrays;

public class BinarySearchNoRecur {
    public static void main(String[] args) {
//        int[] nums= ArrCreator.createRandArr(16);
//        MergeSort.sort(nums,0,nums.length-1,new int[nums.length]);
//        System.out.println(Arrays.toString(nums));

        int[] nums=new int[]{19, 25, 126, 585, 639, 685, 696, 767, 1010, 1137, 1300, 1358, 1369, 1391, 1476, 1569};
        int target=1369;
        int search = search(nums, target);
        System.out.println(search);
    }

    public static int search(int[] nums, int target){
        int left=0;
        int right=nums.length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(nums[mid]==target){
                return mid;
            }else if(nums[mid]>target){
                right=mid-1;
            }else{
                left=mid+1;
            }
        }
        return -1;
    }
}
